package com.it.jnxy.Controller;

import com.it.jnxy.Pojo.Result;
import com.it.jnxy.Pojo.User;
import com.it.jnxy.Service.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.it.jnxy.utils.JWTUtils;
import java.util.HashMap;

@RestController //接口方法返回对象 转换为json文本
//@RequestMapping("/user")  //localhost:8080/user/**
public class LoginController {
        @Autowired
        LoginService loginService;
        //登录
        @PostMapping("/login")
        public Result login(@RequestBody User user) {
                User loginmsg = loginService.Login(user);
                //登陆成功下发令牌
                if (loginmsg != null) {

                       HashMap<String, Object> claims = new HashMap<>();
                       claims.put("username",loginmsg.getUsername());
                       claims.put("password",loginmsg.getPassword());
                       claims.put("id",loginmsg.getId());
                       String jwt = JWTUtils.generateJwt(claims);
                       return Result.sucess(jwt);

                }
                return Result.error("用户名或密码错误");
        }


}
